//
//  S1mpl3FocusCollectionViewCell.m
//  沙哈拉FM
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 S1mpl3. All rights reserved.
//

#import "S1mpl3FocusCollectionViewCell.h"
#import "FitHeader.h"
#import "UIImageView+WebCache.h"
@interface S1mpl3FocusCollectionViewCell ()<UIScrollViewDelegate>
@property(nonatomic,assign)NSInteger num;
@property(nonatomic,assign)NSInteger page;
@end



@implementation S1mpl3FocusCollectionViewCell

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 375 * FITWidth, 200 * FITHeight)];
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(jump)];
            [_scrollView addGestureRecognizer:tap];
            [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(scrollerViewAction) userInfo:nil repeats:YES];
            [self.contentView addSubview:_scrollView];
        });
    }
    return self;
}

-(void)setDataSourceArray:(NSMutableArray *)dataSourceArray{
    if (_dataSourceArray != dataSourceArray) {
        _dataSourceArray = dataSourceArray;
    }
    _scrollView.contentSize = CGSizeMake(375 * FITWidth * (dataSourceArray.count + 2), 200 * FITHeight);
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.pagingEnabled = YES;
    _scrollView.userInteractionEnabled = YES;
    _scrollView.contentOffset = CGPointMake(375 * FITWidth, 0);
    _num = dataSourceArray.count;
    for (NSInteger i = 0; i < dataSourceArray.count + 2; i++) {
        if (i < 1) {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 375 * FITWidth, 200 * FITHeight)];
            [imageView sd_setImageWithURL:[NSURL URLWithString:[[dataSourceArray lastObject] pic]] placeholderImage:[UIImage imageNamed:@"100*100"]];
            [_scrollView addSubview:imageView];
        }else if (i >= 1 && i < _num + 1){
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(375 * FITWidth * i, 0, 375 * FITWidth , 200 * FITHeight)];
            [imageView sd_setImageWithURL:[NSURL URLWithString:[[dataSourceArray objectAtIndex:i - 1] pic]] placeholderImage:[UIImage imageNamed:@"100*100"]];
            [_scrollView addSubview:imageView];
            
        }else{
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(375 * FITWidth * i, 0, 375 * FITWidth, 200 * FITHeight)];
            [imageView sd_setImageWithURL:[NSURL URLWithString:[[dataSourceArray firstObject] pic]] placeholderImage:[UIImage imageNamed:@"100*100"]];
            [_scrollView addSubview:imageView];
            
        }
    }
    _page = 2;
    _scrollView.delegate = self;
    
    
    //    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    
}

-(void)scrollerViewAction{
    //    NSLog(@"%ld--%ld", _page, _num);
    [UIView animateWithDuration:.5 animations:^{
        _scrollView.contentOffset = CGPointMake(_page * 375 * FITWidth, 0);
    }];
    if (_scrollView.contentOffset.x / (375 * FITWidth) == _num + 1) {
        _page = 1;
        _scrollView.contentOffset = CGPointMake(_page * 375 * FITWidth, 0);
        _page++;
    }else
        _page++;
}

#pragma mark -- 代理方法

-(void)jump{
    NSInteger page = _scrollView.contentOffset.x / (375 * FITWidth) - 1;
    //    NSMutableArray *array = [NSMutableArray arrayWithArray:_dataSourceArray];
    //    [array addObject:[_dataSourceArray firstObject]];
    //    [array insertObject:[_dataSourceArray lastObject] atIndex:0];
    [self.delegate sendMessage:page];
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
